﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using DevComponents.Editors.DateTimeAdv;
using QLTB.Controllers;
using QuanLyThietBi.Manager;
using QuanLyThietBi.ManagerBase;
using QuanLyThietBi.HamXuLy;
using QuanLyThietBi.Entity;


namespace QLTB.Forms
{
    public partial class FrmThietBi : Form
    {
        #region Fields
        FrmEditThietBi m_FrmEditThietBi = null;

        #endregion
        public FrmThietBi()
        {
            InitializeComponent();
        }

        private void FrmThietBi_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
       

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            HamXuLy GetID = new HamXuLy();
            string sMaTB = GetID.LayMaID("P_GetMaThietBi");
            m_FrmEditThietBi = new FrmEditThietBi(sMaTB);
            m_FrmEditThietBi.ThemEventThietbi += new EventHandler(Them_Event);
            m_FrmEditThietBi.Text = "Thêm mới thiết bị kỹ thuật";
            m_FrmEditThietBi.Show();
        }
        private void Them_Event(object sender, EventArgs e)
        {
            LoadForm();
        }
        private void bindingNavigatorExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bindingNavigatorEditItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedCellCollection dtselect = dgvThietBi.SelectedCells;
            m_FrmEditThietBi = new FrmEditThietBi(dtselect[1].Value.ToString());
            m_FrmEditThietBi.ThemEventThietbi += new EventHandler(Them_Event);
            m_FrmEditThietBi.Text = "Hiệu chỉnh thông tin thiết bị kỹ thuật";
            m_FrmEditThietBi.Show();
        }
       
        private void LoadForm()
        {
            DataTable dtThietBi = TBL_ThietBiManager.CreateInstant().LayDanhSachThietBi();
            if (dtThietBi.Rows.Count >= 0)
            {
                HamXuLy tblThietbi = new HamXuLy();
                tblThietbi.LoadData(dgvThietBi, bnThietBi, dtThietBi);
            }
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in dgvThietBi.SelectedRows)
                {
                   
                    TBL_ThietBiManager.CreateInstant().DeleteByMaTB(row.Cells[2].Value.ToString());


                }
            }
            LoadForm();
        }

        private void btnrefresh_Click(object sender, EventArgs e)
        {
            LoadForm();
        }
     
    }
}
